<?php
/**
 * @Description mysql配置
 * @Sonkwl Xiong
 * @Date 2022/03/25 13:40:49
 */
namespace Sonkwl\UsePHP;

use \Sonkwl\UsePHP\jonser;

class mysqler{
    /**
     * 基本参数
     */
    private static $PDO="";
	private static $user="";
	private static $pwd="";
	private static $host="";
	private static $dbname="";

	public static function Set($key,$value){
		if(!isset(self::$$key)){
			jsoner::Response(array("res"=>"NG","message"=>"系统没有定义{$key}"));
		}
		self::$$key=$value;
	}

	public static function Get($key){
		if($key=="PDO" && self::$PDO==""){
			return self::$PDO="mysql:host=".self::Get("host").";dbname=".self::Get("dbname")."";
		}
		if(self::$$key==""){
			jsoner::Response(array("res"=>"NG","message"=>"DB没有定义{$key}"));
		}
		return self::$$key;
	}

	public static function SetPDO($str){
		self::$PDO=$str;
	}
	public static function GetPDOString(){
		self::$PDO="mysql:host=".self::Get("host").";dbname=".self::Get("dbname")."";
		return self::Get("PDO");
	}

	public static function SetUser($str){
		self::$user=$str;
	}
	public static function GetUser(){
		return self::$user;
	}

	public static function SetPWD($str){
		self::$pwd=$str;
	}
	public static function GetPWD(){
		return self::$pwd;
	}


    /**
     * PDO配置
     */
	static public function GetPDO(){
		try{
			// echo self::$PDO;
			$db=new \PDO(self::Get("PDO"),self::Get("user"),self::Get("pwd"),array(\PDO::ATTR_EMULATE_PREPARES => false));
			return $db;
		}catch(\PDOException $e){
            // logger::Set($e);
			jsoner::Response(array("res"=>"NG","message"=>$e->getMessage()));
			exit;
		}
		
	}

	//获得自增ID
	static public function GetAutoIncrement($tbl){
		try{
			$dbh=self::GetPDO()->prepare("select auto_increment from information_schema.TABLES where TABLE_SCHEMA=? and table_name=?;");
			$dbh->execute(array($tbl,self::Get("dbname")));
			$rs=$dbh->fetchAll(PDO::FETCH_NUM);
        	return $rs[0][0];
		}catch(\PDOException $e){
            // logger::Set($e);
			jsoner::Response(array("res"=>"NG","message"=>$e->getMessage()));
			exit;
		}
	}
}
?>